

window.onload =async  function() {
    await loadHeader();
    await loadBottom();
    setupLoginModal();
    returnReport();
    returnHome();
};



function loadHeader() {
    fetch('/static/header.html')
        .then(response => response.text())
        .then(data => {
            document.getElementById('header').innerHTML = data;
        })
        .catch(error => console.error('加载header时出错：', error));
}

function loadBottom() {
    return fetch('/static/bottom.html')
        .then(response => response.text())
        .then(data => {
            document.getElementById('bottom').innerHTML = data;
        })
        .catch(error => console.error('加载bottom时出错：', error));
}

function setupLoginModal() {
    // 获取底部导航中的登录按钮
    const loginNavItem = document.querySelector('.bottom-nav .nav-item:last-child');
    if (loginNavItem) {
        loginNavItem.addEventListener('click', showLoginModal);
    }

    // 获取关闭按钮
    const closeLoginBtn = document.getElementById('closeLogin');
    if (closeLoginBtn) {
        closeLoginBtn.addEventListener('click', hideLoginModal);
    }

    // 点击模态框背景关闭
    const loginModal = document.getElementById('loginModal');
    if (loginModal) {
        loginModal.addEventListener('click', function(event) {
            if (event.target === loginModal) {
                hideLoginModal();
            }
        });
    }
}


function showLoginModal() {
    const modal = document.getElementById('loginModal');
    if (modal) {
        modal.style.display = 'block';
    }
}


function hideLoginModal() {
    const modal = document.getElementById('loginModal');
    if (modal) {
        modal.style.display = 'none';
    }
}


function returnReport(){
const reportBottomNav = document.querySelector('.bottom-nav .nav-item:nth-of-type(2)');
if (reportBottomNav) {
     reportBottomNav.addEventListener('click', function(){
        // 对 loginNavItem 进行操作

    window.location.href = '/report';
     });
     }
}




function returnHome(){
const homeBottomNav = document.querySelector('.bottom-nav .nav-item:nth-of-type(1)');
if (homeBottomNav) {
     homeBottomNav.addEventListener('click', function(){
    window.location.href = '/';
     });
     }
}
















// ======================
        // 将全部逻辑包裹在load事件中
// ======================
window.addEventListener('load', function() {

    // 变量声明
    let lastScrollPosition = window.scrollY;
    const nav = document.getElementById('bottom');
    let isHidden = false;


    // 处理滚动函数
    function handleScroll() {
        const currentScroll = window.scrollY;
        const scrollingDown = currentScroll > lastScrollPosition;
        lastScrollPosition = currentScroll;
        if (isNearBottom()) {
            if (!isHidden){
             nav.style.display = 'none';
             isHidden = true;
            }
        } else {
            if (isHidden){
              nav.style.display = 'block';
              isHidden = false;
             }
        }
    }

    // 辅助函数
    function isNearBottom(threshold = 20) {
        return window.innerHeight + window.scrollY >=
            document.body.offsetHeight - threshold;
    }

    // 初始化执行
    handleScroll(); // 确保初始状态正确
    window.addEventListener('scroll', throttle(handleScroll, 100));
});

// 节流函数保持在外层（可选）
function throttle(fn, delay) {
    let timer = null;
    return function() {
        if (!timer) {
            timer = setTimeout(() => {
                fn.apply(this, arguments);
                timer = null;
            }, delay);
        }
    };
}





